package ${domain}.frame.utils;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * ResponseUtil
 *
 * @author wangbing
 * @version 0.0.1
 * @since 2017-01-01
 */
public class ResponseUtil {

    /**
     * 包装ResponseEntity
     *
     * @param bytes    字节数组
     * @param fileName 文件名
     * @return
     */
    public static ResponseEntity<byte[]> apply(byte[] bytes, String fileName) {
        try {
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            headers.setContentDispositionFormData("attachment", URLEncoder.encode(fileName, "utf-8"));
            return new ResponseEntity<>(bytes, headers, HttpStatus.OK);
        } catch (UnsupportedEncodingException e) {
            return null;
        }
    }

    public static ResponseEntity<byte[]> apply(${domain}.frame.excel.WExcel sheet) {
        return apply(sheet.getBytes(), sheet.getName() + ".xlsx");
    }
}
